package org.example.ui.log_registration;
import com.google.gson.Gson;
import com.google.gson.GsonBuilder;
import org.example.business.LoginBusiness;
import org.example.business.UserBusiness;
import org.example.manage.UserManage;
import javax.swing.*;
import javax.swing.event.ChangeEvent;
import javax.swing.event.ChangeListener;
import java.awt.*;
import java.awt.event.FocusEvent;
import java.awt.event.FocusListener;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.io.*;
import java.util.Arrays;

public class PasswordLoginPanel extends JPanel {
    private final JButton cancel, login;
    private final JTextField account;
    private final JPasswordField password;
    private final JLabel accountText, passwordText, mailboxLogin, registration, loginText;
    private final JCheckBox rememberPassword;
    private final JCheckBox checkBox;
    private final Font font;
    private final CardLayout cardLayout;
    private final JPanel container;
    private final ImageIcon background;
    private UserManage dataBase;
    private class AP {
        String a = "", p = "";
    }
    public PasswordLoginPanel(CardLayout cardLayout, JPanel container) {
        setLayout(null);
        setBounds(0,0,800,600);
        background = new ImageIcon("data/background/微信图片_20241127230557.jpg");
        this.cardLayout = cardLayout;
        this.container = container;
        checkBox = new JCheckBox();
        font = new Font("", Font.BOLD,15);
        login = new JButton("登录");
        cancel = new JButton("取消");
        rememberPassword = new JCheckBox();
        mailboxLogin = new JLabel("邮箱登录");
        registration = new JLabel("注册");
        loginText = new JLabel("密码登录");
        account = new JTextField("邮箱",11);
        password = new JPasswordField(11);
        AP ap = read();
        accountText = new JLabel("邮箱");
        passwordText = new JLabel("密码");
        password.setEchoChar('*');
        setAll();
        setAllListener();
        addAll();
        if (!ap.a.isEmpty()) {
            account.setForeground(Color.BLACK);
            account.setText(ap.a);
            password.setText(ap.p);
        }
        setVisible(true);
    }
    private void setAll() {
        rememberPassword.setText("记住密码");
        mailboxLogin.setFont(font);
        registration.setFont(font);
        checkBox.setFont(font);
        checkBox.setBounds(500,210,50,50);
        checkBox.setContentAreaFilled(false);
        loginText.setFont(new Font("",10,30));
        loginText.setBounds(330,100,200,50);
        registration.setBounds(500,110,100,50);
        mailboxLogin.setBounds(420,260,80,30);
        rememberPassword.setFont(font);
        rememberPassword.setBounds(300,260,120,30);
        rememberPassword.setContentAreaFilled(false);
        login.setFont(font);
        cancel.setFont(font);
        cancel.setBounds(260,320,80,40);
        login.setBounds(450,320,80,40);
        account.setBounds(300,150,200,50);
        account.setFocusable(false);
        account.setFont(font);
        account.setForeground(Color.LIGHT_GRAY);
        accountText.setFont(font);
        accountText.setBounds(250,150,50,50);
        passwordText.setFont(font);
        passwordText.setBounds(250,210,50,50);
        password.setBounds(300,210,200,50);
        password.setFont(font);
        password.setFocusable(false);
    }

    @Override
    public void paintComponent(Graphics g) {
        g.drawImage(background.getImage(),0,0,800,600,null);
    }
    private void setAllListener() {
        account.addMouseListener(new MouseAdapter() {
            @Override
            public void mouseClicked(MouseEvent e) {
                account.setFocusable(true);
            }
        });
        account.addFocusListener(new FocusListener() {
            @Override
            public void focusGained(FocusEvent e) {
                if(account.getText().equals("邮箱")) {
                    account.setText("");
                    account.setForeground(Color.BLACK);
                }
            }
            @Override
            public void focusLost(FocusEvent e) {
                if(account.getText().isEmpty()) {
                    account.setText("邮箱");
                    account.setForeground(Color.LIGHT_GRAY);
                }
            }
        });
        password.addMouseListener(new MouseAdapter() {
            @Override
            public void mouseClicked(MouseEvent e) {
                password.setFocusable(true);
            }
        });
        mailboxLogin.addMouseListener(new MouseAdapter() {
            @Override
            public void mouseClicked(MouseEvent e) {
                cardLayout.show(container,"2");
            }
        });
       registration.addMouseListener(new MouseAdapter() {
           @Override
           public void mouseClicked(MouseEvent e) {
               cardLayout.show(container,"0");
           }
       });
       login.addMouseListener(new MouseAdapter() {
           @Override
           public void mouseClicked(MouseEvent e) {
               if(UserBusiness.passwordLogin(PasswordLoginPanel.this, account.getText(), Arrays.toString(password.getPassword()))){
                   if (rememberPassword.isSelected()){
                       save();
                   }

                   JOptionPane.showMessageDialog(PasswordLoginPanel.this, "登录成功");
                   LoginBusiness.dialog.setVisible(false);
                   account.setForeground(Color.BLACK);
                   account.setText(read().a);
                   password.setText(read().p);
               }
           }
       });
       checkBox.addChangeListener(new ChangeListener() {
           @Override
           public void stateChanged(ChangeEvent e) {
               if (checkBox.isSelected()) {
                   password.setEchoChar((char)0);
               }
               else password.setEchoChar('*');
           }
       });
        cancel.addMouseListener(new MouseAdapter() {
            @Override
            public void mouseClicked(MouseEvent e) {
                System.exit(0);
            }
        });
    }
    private void addAll() {
        add(mailboxLogin);
        add(checkBox);
        add(account);
        add(rememberPassword);
        add(passwordText);
        add(accountText);
        add(registration);
        add(login);
        add(cancel);
        add(password);
        add(loginText);
    }
    private AP read() {
        File file = new File("data/user_json/json.json");
        Gson gson = new GsonBuilder().setPrettyPrinting().create();
        String json = "";
        try {
            FileReader reader = new FileReader(file.getAbsoluteFile());
            BufferedReader bufferedReader = new BufferedReader(reader);
            while(true){
                int n = bufferedReader.read();
                if(n!=-1){
                    json = String.format("%s%c", json,(char)n);
                }
                else break;
            }
        } catch (FileNotFoundException e) {
            throw new RuntimeException(e);
        } catch (IOException e) {
            throw new RuntimeException(e);
        }
        return new Gson().fromJson(json, AP.class);
    }
    private void save() {
        AP ap = new AP();
        ap.a = account.getText();
        for (char ch : password.getPassword()) {
            ap.p = ap.p + ch;
        }
        Gson gson = new GsonBuilder().setPrettyPrinting().create();
        String json = gson.toJson(ap);
        File file = new File("data/user_json/json.json");
        try {
            FileWriter writer = new FileWriter(file.getAbsoluteFile(),false);
            BufferedWriter bufferedWriter = new BufferedWriter(writer);
            bufferedWriter.write(json);
            bufferedWriter.close();
        } catch (IOException e) {
            throw new RuntimeException(e);
        }
    }
}
